<?php

	// +----------------------------------------------------------------------
	// | 文档模型控制器
	// | 文档模型列表和详情.
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\cms;

	use app\event\cms\Document as DocumentEvent;
	use app\event\cms\Document as DocumentModelEvent;
	use app\model\cms\Category as CategoryModel;
	use app\model\cms\Document as DocumentModel;

	class Document extends Common
	{

		/* 文档模型频道页 */
		public function index()
		{
			/* 分类信息 */
			$category = $this->category();
			// 频道页只显示模板，默认不读取任何内容
			// 内容可以通过模板标签自行定制
			/* 模板赋值并渲染模板 */
			$this->assign('category', $category);
			return $this->fetch($category['template_index']);
		}

		/* 文档模型列表页 */
		public function lists()
		{
			if (1 == $this->request->param('more', 0)) {
				$data = DocumentModelEvent::lists($this->request->param('category_id'));
				return json($data);
			} else {
				$this->assign('c_menu', 'news');
				/* 分类信息 */
				$category_select = $this->category();
				/* 模板赋值并渲染模板 */
				$this->assign('category', $category_select);
				if ($category_select['style'] == 2) { // 终极列表
					// 顶级
					$order = $this->request->get('order', 'news');
					if ($order == 'news') {
						$sort = 'create_time DESC';
					} elseif ('download' == $order) {
						$sort = 'download DESC';
					} elseif ('recommend' == $order) {
						$sort = 'view DESC';
					} else {
						$sort = 'create_time DESC';
					}
					$this->assign('order', $order);
					$this->assign('sort', $sort);
					/* 获取当前分类列表 */
					$result = DocumentEvent::lists($category_select['id'], 10, $sort);
					if (0 != $result['code']) {
						$this->error($result['message']);
					}
					$this->assign('lists', $result['data']);
					$this->assign('page', $result['data']->render());
				}
				/* 获取当前分类列表 */
				$nav_category = CategoryModel::where('pid', $category_select['pid'])
					->where('display', 1)
					->order('sort ASC')
					->where('style', 2)
					->where('status', 1)
					->select();
				$this->assign('nav_category', $nav_category);
				if ($category_select['pid']) {
					$this->assign('nav_category_select', CategoryModel::find($category_select['pid']));
				} else {
					$this->assign('nav_category_select', $category_select);
				}
				// 寻找顶级菜单
				if (0 == $category_select['pid']) {
					$this->assign('category_present', $category_select);
				} else {
					$this->assign('category_present', CategoryModel::find($category_select['pid']));
				}
				$this->assign('category_id', $this->request->param('category_id'));
				/* 获取列表模版路径 */
				if (trim($category_select['template_lists'])) {
					$template_file = $category_select['template_lists'];
				} else {
					$template_file = $category_select['name'];
				}
				return $this->fetch('document/lists/news');
//				return $this->fetch('document/lists/' . $template_file);
			}

		}


		/* 文档模型详情页 */
		public function detail()
		{
			$this->assign('c_menu', 'news');
			/* 获取详细信息 */
			$event = new DocumentEvent();
			$result = $event->detail($this->request->param(), $this->user);
			if (0 != $result['code']) {
				$this->error($result['message']);
			} else {
				$document = $result['data'];
				/* 分类信息 */
				$category_select = $this->category();
				/* 获取模板 */
				if (!empty(trim($document['template_detail']))) { // 自定义时间模版
					$tmpl = $document['template'];
				} elseif (!empty($category_select['template_detail'])) { // 分类定制模板
					$tmpl = 'document/detail/' . $category_select['template_detail'];
				} elseif (!empty($document->document_model->template_detail)) { // 模型定制模板
					$tmpl = 'document/detail/' . $document->pattern->template_detail;
				} else { // 使用默认模板
					$tmpl = 'document/detail/' . $document->pattern->name;
				}
				if ($category_select['id'] == 19) {
					$nav_lists = CategoryModel::where('pid', $category_select['pid'])->where('status', 1)->select();
					$this->assign('nav_lists', $nav_lists);
				}
				/* 模板赋值并渲染模板 */
				$this->assign('category_select', $category_select);
				$this->assign('data', $document);
//				return $this->fetch($tmpl);
				return $this->fetch('document/detail/article');
			}
		}


		public function tags()
		{
			$this->assign('tag', $this->request->param('tag'));
			return $this->fetch();
		}

		public function search()
		{
			// dump($_GET);
		}


		public function app()
		{
			return $this->fetch();
		}

		public function contact()
		{
			$this->assign('c_menu', 'contact');
			$this->setSeo('联系我们');
			return $this->fetch();
		}

		public function about()
		{
			$this->assign('c_menu', 'about');
			$this->setSeo('联系我们');
			return $this->fetch();
		}

		/**
		 * 增加阅读量返回当前阅读量.
		 * @param int $id
		 * @return string
		 */
		public function view($id = 0)
		{
			if ($id) {
				$document = DocumentModel::where('id', $id)->find();
				if ($document) {
					/* 更新浏览数 */
					$document['view'] = $document['view'] + 1;
					$document->save();
					return 'ok';
				} else {
					return 'err';
				}
			} else {
				return 'err';
			}
		}
	}
